﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using Kang.DbDoc.Interfaces;
using Kang.DbDoc.Models;
using Kang.ExtendMethod;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kang.DbDoc.ServicesImpl
{
    public class PdfExportService : IExport
    {
        //设置字体
        static BaseFont baseFont = BaseFont.CreateFont(Path.Combine(System.Environment.CurrentDirectory, @"Fonts\SIMSUN.TTC") + ",0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        public bool Export(string filename, DatabaseModel database, List<TableOrViewModel> tables, List<TableOrViewModel> views, List<RoutineModel> functions, List<RoutineModel> procedures, ref string error)
        {
            Rectangle rectangle = null;
            Document document = null;
            PdfWriter pdfWriter = null;
            PdfPTable table = null;
            PdfPCell cell = null;
            //段落对象
            Paragraph paragraph = null;
            Phrase phrase = null;
            Image image = null;
            Chunk chunk = null;
            Font font = new Font(baseFont, 9);
            Font newLineFont = new Font(baseFont, 4);
            Font boldFont = new Font(baseFont, 9, Font.BOLD);
            Font titleFont = new Font(baseFont, 14, Font.BOLD);
            Font subTitleFont = new Font(baseFont, 12, Font.BOLD);
            try
            {
                rectangle = new Rectangle(PageSize.A4);
                document = new Document(rectangle, 20, 20, 20, 20);
                pdfWriter = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));

                document.Open();
                //这里是标题
                document.AddTitle($"{database.DatabaseName}表结构文档");
                //主题
                document.AddSubject("表结构文档");
                //关键字
                document.AddKeywords($"数据库,表结构,表信息");
                //创建者
                document.AddCreator("Kang.ReDoc");
                //作者
                document.AddAuthor("Kang.ReDoc");

                if (database.DatabaseType == Enums.DatabaseTypeEnum.SQLite)
                {
                    paragraph = new Paragraph($"{Path.GetFileName(database.DatabaseName)}表结构文档", titleFont);
                }
                else
                {
                    paragraph = new Paragraph($"{database.DatabaseName}表结构文档", titleFont);
                }
                paragraph.Alignment = 1;
                document.Add(paragraph);

                paragraph = new Paragraph($"一、数据库信息", subTitleFont);
                document.Add(paragraph);
                if (database.DatabaseType == Enums.DatabaseTypeEnum.SQLite)
                {
                    paragraph = new Paragraph($"地址：{database.DatabaseName}", subTitleFont);
                }
                else
                {
                    paragraph = new Paragraph($"地址：{database.DatabaseHost}", subTitleFont);
                }
                document.Add(paragraph);
                if (database.DatabasePort > 0)
                {
                    paragraph = new Paragraph($"端口：{database.DatabasePort}", subTitleFont);
                    document.Add(paragraph);
                }
                paragraph = new Paragraph($"总表数：{tables.Count}", subTitleFont);
                document.Add(paragraph);
                paragraph = new Paragraph($"二、表信息", subTitleFont);
                document.Add(paragraph);

                int t_i = 0;
                foreach (var tableData in tables)
                {
                    if (t_i > 0)
                    {
                        paragraph = new Paragraph($"{Environment.NewLine}表：{tableData.Name}", boldFont);
                    }
                    else
                    {
                        paragraph = new Paragraph($"表：{tableData.Name}", boldFont);
                    }
                    paragraph.Alignment = 1;
                    document.Add(paragraph);

                    if (tableData.Comment.IsNotBlank())
                    {
                        paragraph = new Paragraph($"注解：{tableData.Comment}", font);
                        document.Add(paragraph);
                    }
                    paragraph = new Paragraph($"{Environment.NewLine}", newLineFont);
                    document.Add(paragraph);

                    table = new PdfPTable(10);
                    table.TotalWidth = PageSize.A4.Width - 20;
                    table.LockedWidth = true;
                    table.SetTotalWidth(new float[] { 23, 183, 55, 28, 23, 42, 23, 23, 50, 110 });

                    cell = new PdfPCell(phrase = new Phrase("序号", boldFont));
                    cell.BorderWidth = 1;
                    cell.PaddingBottom = 3;
                    table.AddCell(cell);
                    cell = new PdfPCell(phrase = new Phrase("列名", boldFont));
                    cell.BorderWidth = 1;
                    cell.PaddingBottom = 3;
                    table.AddCell(cell);
                    cell = new PdfPCell(phrase = new Phrase("数据类型", boldFont));
                    cell.BorderWidth = 1;
                    cell.PaddingBottom = 3;
                    table.AddCell(cell);
                    cell = new PdfPCell(phrase = new Phrase("长度", boldFont));
                    cell.BorderWidth = 1;
                    cell.PaddingBottom = 3;
                    table.AddCell(cell);
                    cell = new PdfPCell(phrase = new Phrase("小数", boldFont));
                    cell.BorderWidth = 1;
                    cell.PaddingBottom = 3;
                    table.AddCell(cell);
                    cell = new PdfPCell(phrase = new Phrase("键类型", boldFont));
                    cell.BorderWidth = 1;
                    cell.PaddingBottom = 3;
                    table.AddCell(cell);
                    cell = new PdfPCell(phrase = new Phrase("自增", boldFont));
                    cell.BorderWidth = 1;
                    cell.PaddingBottom = 3;
                    table.AddCell(cell);
                    cell = new PdfPCell(phrase = new Phrase("为空", boldFont));
                    cell.BorderWidth = 1;
                    cell.PaddingBottom = 3;
                    table.AddCell(cell);
                    cell = new PdfPCell(phrase = new Phrase("默认", boldFont));
                    cell.BorderWidth = 1;
                    cell.PaddingBottom = 3;
                    table.AddCell(cell);
                    cell = new PdfPCell(phrase = new Phrase("列说明", boldFont));
                    cell.BorderWidth = 1;
                    cell.PaddingBottom = 3;
                    table.AddCell(cell);

                    int i = 1;
                    foreach (var colData in tableData.Columns)
                    {
                        cell = new PdfPCell(phrase = new Phrase($"{i++}", font));
                        cell.BorderWidth = 1;
                        cell.PaddingBottom = 4;
                        table.AddCell(cell);
                        cell = new PdfPCell(phrase = new Phrase($"{colData.Name}", font));
                        cell.BorderWidth = 1;
                        cell.PaddingBottom = 4;
                        table.AddCell(cell);
                        cell = new PdfPCell(phrase = new Phrase($"{colData.Type}", font));
                        cell.BorderWidth = 1;
                        cell.PaddingBottom = 4;
                        table.AddCell(cell);
                        cell = new PdfPCell(phrase = new Phrase($"{colData.Length}", font));
                        cell.BorderWidth = 1;
                        cell.PaddingBottom = 4;
                        table.AddCell(cell);
                        cell = new PdfPCell(phrase = new Phrase($"{colData.Scale}", font));
                        cell.BorderWidth = 1;
                        cell.PaddingBottom = 4;
                        table.AddCell(cell);
                        cell = new PdfPCell(phrase = new Phrase($"{colData.KeyType}", font));
                        cell.HorizontalAlignment = 1;
                        cell.BorderWidth = 1;
                        cell.PaddingBottom = 4;
                        table.AddCell(cell);
                        cell = new PdfPCell(phrase = new Phrase($"{(colData.IsAutoIncrement ? "√" : "")}", font));
                        cell.HorizontalAlignment = 1;
                        cell.BorderWidth = 1;
                        cell.PaddingBottom = 4;
                        table.AddCell(cell);
                        cell = new PdfPCell(phrase = new Phrase($"{(colData.IsNull ? "√" : "")}", font));
                        cell.HorizontalAlignment = 1;
                        cell.BorderWidth = 1;
                        cell.PaddingBottom = 4;
                        table.AddCell(cell);
                        cell = new PdfPCell(phrase = new Phrase($"{colData.Default}", font));
                        cell.BorderWidth = 1;
                        cell.PaddingBottom = 4;
                        table.AddCell(cell);
                        cell = new PdfPCell(phrase = new Phrase($"{colData.Comment}", font));
                        cell.BorderWidth = 1;
                        cell.PaddingBottom = 4;
                        table.AddCell(cell);
                    }
                    document.Add(table);
                }

                // 关闭Pdf文件
                document.Dispose();
                return true;
            }
            catch (Exception e)
            {
                error = e.Message;
                return false;
            }
        }
    }
}
